<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.IOException"%>
<%@page import="java.text.ParseException"%>
<%@page import="java.sql.Date"%>
<%@page import="by.epam.communalservices.entity.JkhTask"%>
<%@page import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhTask"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.text.SimpleDateFormat"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib tagdir="/WEB-INF/tags/connect" prefix="connect"%>
<%@ taglib tagdir="/WEB-INF/tags/object" prefix="object"%>
<%@ taglib tagdir="/WEB-INF/tags/table" prefix="table"%>
<%@ taglib tagdir="/WEB-INF/tags/task" prefix="task"%>
<%@ taglib tagdir="/WEB-INF/tags/team" prefix="team"%>

<connect:isset/>
<connect:denny rules="admin,edit,task_edit,task_view,objecter,tasker,brigadir,kadrovik" />

<% pageContext.setAttribute("newLineChar", "\n"); %> 

<connect:rule val="admin" rules="admin,editor,task_edit" />
<connect:rule val="objecter" rules="objecter" />
<connect:rule val="tasker" rules="tasker" />
<connect:rule val="brigadir" rules="brigadir" />


<task:edit id="${param.id}" idObject ="${param.idObject}"
	idTeam ="${param.idTeam}" jobDescription="${param.jobDescription}"
	performTime="${param.performTime}" status="${param.status}" jobRequest="${param.jobRequest}"
	submit="${param.submit}" delete="${param.delete}" />


<c:if test="${requestScope.message ne ''}">
	<p class='message'>
		<c:out value="${message}" />
	</p>
</c:if>
<c:if test="${param.delete gt 0}">
	<c:import url="/WEB-INF/pages/task_view.jsp" />
</c:if>
<c:if test="${form}">


	<form method='POST' action='task_edit'>
		<table:body style='width:70%'>
			<table:title span="4">
				<c:choose>
					<c:when test="${param.id gt 0}">
      				Редактирование задания
  				</c:when>
					<c:otherwise>
      				Добавление задания
  				</c:otherwise>
				</c:choose>
			</table:title>

			<input name='id' value="${requestScope.task.id}" type="hidden">

			<tr id=c>
				<td colspan=2 class=t>Код объекта  недвижимости</td>
				<td colspan=2 class=t id=l>
				<c:choose>
					<c:when test="${admin}">
						<object:select name="idObject" selected="${requestScope.task.idObject}" attr="style='width:160px;'" />
					</c:when>
					<c:when test="${objecter}">
						<input type='hidden' name="idObject" value="${requestScope.task.idObject}" />
						<object:link id="${requestScope.task.idObject}"/>
					</c:when>
					<c:otherwise>
						<object:link id="${requestScope.task.idObject}"/>
						
					</c:otherwise>
				</c:choose>					
				</td>
			</tr>

			<tr id=n>
				<td colspan=2 class=t>Код бригады</td>
				<td colspan=2 class=t id=l>
				<c:choose>
					<c:when test="${admin}">
						<team:select name="idTeam" selected="${requestScope.task.idTeam}" attr="style='width:160px;'" none='yes' />
					</c:when>
					<c:when test="${objecter and requestScope.task.status eq 0}">
						<input type='hidden' name="idObject" value="${requestScope.task.idTeam}" />
						<team:link id="${requestScope.task.idTeam}"/>
					</c:when>
					<c:when test="${tasker and requestScope.task.status lt 3}">
						<team:select name="idTeam" selected="${requestScope.task.idTeam}" attr="style='width:160px;'" none='yes' />
					</c:when>
					<c:otherwise>
						<team:link id="${requestScope.task.idTeam}" owner="brigadirId" />
						<c:set var="brigadir" value="${brigadir and brigadirId eq sessionScope.currentUser.id}" />
					</c:otherwise>
				</c:choose>					
				</td>
			</tr>

			<tr id=c>
				<td colspan=2 class=t>Описание задания</td>
				<td colspan=2 id=l>
				<c:choose>
					<c:when test="${admin or objecter and requestScope.task.status eq 0}">
						<textarea name='jobDescription' style='width:400px;height:100px;'><c:out value="${fn:replace(requestScope.task.jobDescription,'<br>',newLineChar)}" escapeXml="false" /></textarea> 
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.task.jobDescription}" escapeXml="false" />
					</c:otherwise>
				</c:choose>					
				</td>
			</tr>

			<tr id=n>
				<td colspan=2 class=t>Количество часов на выполнение</td>
				<td colspan=2 class=t id=l>
				<c:choose>
					<c:when test="${admin or tasker and requestScope.task.status lt 2}">
						<input name='performTime' value="${requestScope.task.performTime}" />
					</c:when>
					<c:when test="${requestScope.task.performTime eq '' or empty requestScope.task.performTime}">
						?<input name='performTime' type='hidden' value="0" />
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.task.performTime}" escapeXml="false" />
					</c:otherwise>
				</c:choose>					
				
				</td>
			</tr>

			<tr id=c>
				<td colspan=2 class=t>Статус</td>
				<td colspan=2 class=t id=l>
				<c:choose>
					<c:when test="${admin}">
						<table:select items="новое,принято,отказано,выполнено,завершено" name="status" attr="style='width:160px;'"
							selected="${requestScope.task.status}" />
					</c:when>
					<c:when test="${objecter and requestScope.task.status eq 3}">
						<table:select items="новое,принято,отказано,выполнено,завершено" name="status" attr="style='width:160px;'"
							selected="${requestScope.task.status}" disabled="0,1,2,3"/>
					</c:when>
					<c:when test="${tasker and requestScope.task.status lt 3}">
						<table:select items="новое,принято,отказано,выполнено,завершено" name="status" attr="style='width:160px;'"
							selected="${requestScope.task.status}" disabled="0,2,3,4" />
					</c:when>
					<c:when test="${brigadir and requestScope.task.status eq 1}">
						<table:select items="новое,принято,отказано,выполнено,завершено" name="status" attr="style='width:160px;'"
							selected="${requestScope.task.status}"  disabled="0,1,4" />
					</c:when>
					<c:otherwise>
						<table:select items="новое,принято,отказано,выполнено,завершено" name="status" attr="style='width:160px;'"
							selected="${requestScope.task.status}" blocked="true"/>
					</c:otherwise>
				</c:choose>
				</td>
			</tr>

			<tr id=n>
				<td colspan=2 class=t>Коментарии</td>
				<td colspan=2 id=l>
				<c:choose>
					<c:when test="${admin or brigadir and requestScope.task.status gt 0 and requestScope.task.status lt 4}">
						<textarea name='jobRequest' style='width:400px;height:100px;'><c:out value="${fn:replace(requestScope.task.request,'<br>',newLineChar)}" escapeXml="false" /></textarea> 
					</c:when>
					<c:otherwise>
						<c:out value="${requestScope.task.request}" escapeXml="false" />
					</c:otherwise>
				</c:choose>					
				</td>
			</tr>

			<table:title span="4" down="yes">
				<c:if test="${admin or objecter or brigadir or tasker}">
					<input name='submit' value='save' type='submit'>
				</c:if>
			</table:title>
		</table:body>
	</form>
</c:if>
